/**
 * @param {number[]} nums
 * @return {number[]}
 */
 var sortArray = function (nums) {
    if (nums === null || !nums.length) return
    return mergeSort(nums, 0, nums.length - 1)
  };
  
  const mergeSort = (nums, left, end) => {
    if (left >= end) return nums
    let mid = Math.floor((left + end) / 2)
    mergeSort(nums, left, mid)
    mergeSort(nums, mid + 1, end)
    return merge(nums, left, mid, end)
  }
  
  const merge = (nums, left, mid, right) => {
    let ans = []
    let index = 0
    let leftIndex = left
    let rightIndex = mid + 1
  
    while (leftIndex <= mid && rightIndex <= right) {
      if (nums[leftIndex] <= nums[rightIndex]) {
        ans[index++] = nums[leftIndex++]
      } else {
        ans[index++] = nums[rightIndex++]
      }
    }
  
    while (leftIndex <= mid) {
      ans[index++] = nums[leftIndex++]
    }
  
    while (rightIndex <= right) {
      ans[index++] = nums[rightIndex++]
    }
  
    for (let i = 0; i < ans.length; ++i) {
      nums[i + left] = ans[i]
    }
    return nums
  }